package uestc.lj.basicPromotion.monstack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 单调栈求一个数组中，某一个数的左右两边离其最近的最大值
 *
 * @Author:Crazlee
 * @Date:2021/11/27
 */
public class Code01_MonotonousStack {

	/**
	 * 数组无重复值
	 *
	 * @param array 数组
	 * @return 数组中任意一元素左边的最近的最大值和右边的最近的最大值
	 */
	public static int[][] getNearestMoreNoRepeat(int[] array) {
		int[][] result = new int[array.length][2];
		//单调递减栈
		Stack<Integer> stack = new Stack<>();

		for (int i = 0; i < array.length; i++) {
			while (!stack.isEmpty() && array[stack.peek()] < array[i]) {
				int popIndex = stack.pop();
				int leftMoreIndex = stack.isEmpty() ? -1 : stack.peek();
				result[popIndex][0] = leftMoreIndex;
				result[popIndex][1] = i;
			}
			stack.push(i);
		}
		while (!stack.isEmpty()) {
			int popIndex = stack.pop();
			int leftMoreIndex = stack.isEmpty() ? -1 : stack.peek();
			result[popIndex][0] = leftMoreIndex;
			result[popIndex][1] = -1;
		}
		return result;
	}

	/**
	 * 数组无重复值
	 *
	 * @param arr 数组
	 * @return 数组中任意元素的左边最近的最小值和右边最近的最小值
	 */
	public static int[][] getNearLessNoRepeat(int[] arr) {
		int[][] res = new int[arr.length][2];
		//单调递增栈
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < arr.length; i++) {
			while (!stack.isEmpty() && arr[stack.peek()] > arr[i]) {
				int popIndex = stack.pop();
				int leftLessIndex = stack.isEmpty() ? -1 : stack.peek();
				res[popIndex][0] = leftLessIndex;
				res[popIndex][1] = i;
			}
			stack.push(i);
		}
		while (!stack.isEmpty()) {
			int popIndex = stack.pop();
			int leftLessIndex = stack.isEmpty() ? -1 : stack.peek();
			res[popIndex][0] = leftLessIndex;
			res[popIndex][1] = -1;
		}
		return res;
	}

	/**
	 * 数组有重复值
	 *
	 * @param array 数组
	 * @return 数组中任意一元素的左边最近的最大值和右边最近的最大值
	 */
	public static int[][] getNearMore(int[] array) {
		int[][] result = new int[array.length][2];
		Stack<List<Integer>> stack = new Stack<>();
		for (int i = 0; i < array.length; i++) {
			while (!stack.isEmpty() && array[stack.peek().get(0)] < array[i]) {
				List<Integer> popIds = stack.pop();
				int leftMoreIndex = stack.isEmpty() ? -1 : stack.peek().get(stack.peek().size() - 1);
				for (Integer popId : popIds) {
					result[popId][0] = leftMoreIndex;
					result[popId][1] = i;
				}
			}
			if (!stack.isEmpty() && array[stack.peek().get(0)] == array[i]) {
				stack.peek().add(i);
			} else {
				ArrayList<Integer> list = new ArrayList<>();
				list.add(i);
				stack.push(list);
			}
		}
		while (!stack.isEmpty()) {
			List<Integer> popIds = stack.pop();
			int leftMoreIndex = stack.isEmpty() ? -1 : stack.peek().get(stack.peek().size() - 1);
			for (Integer popId : popIds) {
				result[popId][0] = leftMoreIndex;
				result[popId][1] = -1;
			}
		}
		return result;
	}

	/**
	 * 数组有重复值
	 *
	 * @param arr 数组
	 * @return 数组中任意元素的左边最近的最小值和右边最近的最小值
	 */
	public static int[][] getNearLess(int[] arr) {
		int[][] res = new int[arr.length][2];
		Stack<List<Integer>> stack = new Stack<>();
		for (int i = 0; i < arr.length; i++) {
			while (!stack.isEmpty() && arr[stack.peek().get(0)] > arr[i]) {
				List<Integer> popIs = stack.pop();
				// 取位于下面位置的列表中，最晚加入的那个
				int leftLessIndex = stack.isEmpty() ? -1 : stack.peek().get(
						stack.peek().size() - 1);
				for (Integer popi : popIs) {
					res[popi][0] = leftLessIndex;
					res[popi][1] = i;
				}
			}
			if (!stack.isEmpty() && arr[stack.peek().get(0)] == arr[i]) {
				stack.peek().add(Integer.valueOf(i));
			} else {
				ArrayList<Integer> list = new ArrayList<>();
				list.add(i);
				stack.push(list);
			}
		}
		while (!stack.isEmpty()) {
			List<Integer> popIs = stack.pop();
			// 取位于下面位置的列表中，最晚加入的那个
			int leftLessIndex = stack.isEmpty() ? -1 : stack.peek().get(
					stack.peek().size() - 1);
			for (Integer popi : popIs) {
				res[popi][0] = leftLessIndex;
				res[popi][1] = -1;
			}
		}
		return res;
	}
}
